home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / gprim / quad / quadnormal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-24  |  1.5 KB  |  58 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips */
  11.  
  12. #include "quadP.h"
  13.  
  14. Quad *
  15. QuadComputeNormals( quad )
  16. Quad *quad;
  17. {
  18.     int q;
  19.     Point3 nor;
  20.     float len;
  21.     register HPoint3 *p;
  22.     register Point3 *n;
  23.  
  24.     /*
  25.      * Only compute if not present!
  26.      */
  27.     if( !(quad->flag & QUAD_N) ) {
  28.     if(quad->n == NULL)
  29.         quad->n = OOGLNewNE(QuadN, quad->maxquad, "QuadComputeNormals normals");
  30.     p = &quad->p[0][0];
  31.     n = &quad->n[0][0];
  32.     for( q = quad->maxquad; --q >= 0; p += 4) {
  33.  
  34. #define  TERM(S, T)    (p[0].S - p[1].S) * (p[1].T + p[0].T) + \
  35.             (p[1].S - p[2].S) * (p[2].T + p[1].T) + \
  36.             (p[2].S - p[3].S) * (p[3].T + p[2].T) + \
  37.             (p[3].S - p[0].S) * (p[0].T + p[3].T)
  38.  
  39.         nor.x = TERM(y, z);
  40.         nor.y = TERM(z, x);
  41.         nor.z = TERM(x, y);
  42.         len = nor.x*nor.x + nor.y*nor.y + nor.z*nor.z;
  43.         if( len != 0.0 ) {
  44.         len = 1.0 / sqrt(len);
  45.         nor.x *= len;
  46.         nor.y *= len;
  47.         nor.z *= len;
  48.         }
  49.         *n++ = nor;
  50.         *n++ = nor;
  51.         *n++ = nor;
  52.         *n++ = nor;
  53.     }
  54.     quad->flag |= QUAD_N;
  55.     }
  56.     return quad;
  57. }
  58.